package com.ray.depot.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.depot.service.StockSerializeService;
import com.ray.depot.table.customMapper.SelectStockSerializeMapper;
import com.ray.depot.table.dto.StockSerializeDto;
import com.ray.depot.table.entity.StockSerialize;
import com.ray.depot.table.entity.StockSerializeExample;
import com.ray.depot.table.entity.StockSerializeExample.Criteria;
import com.ray.depot.table.mapper.StockSerializeMapper;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class StockSerializeServiceImpl extends BaseService implements StockSerializeService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(StockSerializeServiceImpl.class);
	
	@Autowired
	private StockSerializeMapper stockSerializeMapper;
	@Autowired
	private SelectStockSerializeMapper SelectStockSerializeMapper;

	@Transactional
	public Result<Object> addStockSerialize(StockSerialize params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		if(StrUtil.isBlank(params.getId())){
			params.setId(UUIDUtil.getUUID());
		}
		params.setTenantId(user.getTenantId());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count=stockSerializeMapper.insertSelective(params);
		if(count!=1){
			logger.error("StockSerializeServiceImpl=>addStockSerialize is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public StockSerialize queryStockSerialize(String id, LoginUser user) {
		return stockSerializeMapper.selectByPrimaryKey(id);
		}
	
	
    @Transactional
	public Result<Object> updateStockSerialize(StockSerialize params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=stockSerializeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("StockSerializeServiceImpl=>editStockSerialize is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional
	public Result<Object> deleteStockSerialize(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		StockSerialize params=new StockSerialize();
		params.setId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=stockSerializeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("StockSerializeServiceImpl=>deleteStockSerialize is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<StockSerialize> queryStockSerialize(StockSerialize params, PageDto<StockSerialize> page, LoginUser user) {
		StockSerializeExample example=new StockSerializeExample();
		Criteria criteria = example.createCriteria();
		//超级管理员能查看全部
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(stockSerializeMapper.countByExample(example));
		page.setRecords(stockSerializeMapper.selectByExample(example));
		return page;
	}

    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<StockSerializeDto> querySelectStockSerialize(StockSerializeDto params, PageDto<StockSerializeDto> page, LoginUser user) {
	    params.setTenantId(user.getTenantId());
	    params.setOrderByClause(page.getOrderBy());
	    params.setLimitStart((page.getPage() - 1)  * page.getPageSize());
	    params.setLimitEnd(page.getPageSize());
	    params.setIsDelete(DeleteEnum.USE.getValue());
		page.setTotal(SelectStockSerializeMapper.countByExample(params));
		page.setRecords(SelectStockSerializeMapper.selectByExample(params));
		return page;
	}


	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public StockSerialize queryStockSerialize(String materialId, String serializaNo, String warchouseId) {
		StockSerializeExample example = new StockSerializeExample(); 
		example.createCriteria().andWarchouseIdEqualTo(warchouseId)
		.andMaterialIdEqualTo(materialId).andSerializaNoEqualTo(serializaNo)
		.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		List<StockSerialize> list = stockSerializeMapper.selectByExample(example);
		if(list.isEmpty()){
			return null;
		}
		return list.get(0);
	}


	@Override
	@Transactional
	public void inOutSerializeOut(StockSerialize stockSerialize, LoginUser user) {
		stockSerialize.setIsDelete(DeleteEnum.DELETE.getValue());
		stockSerialize.setVersionNo(stockSerialize.getVersionNo()+1);
		this.setUserForUpdate(stockSerialize, user);
		StockSerializeExample example = new StockSerializeExample(); 
		example.createCriteria().andIdEqualTo(stockSerialize.getId())
		.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		int count  = stockSerializeMapper.updateByExampleSelective(stockSerialize, example);
		if(count!=1){
			logger.error("StockSerializeServiceImpl=>inOutSerializeOut is error");
			throw new BussinessException(-1, String.format("资产：%s出库失败.", stockSerialize.getSerializaNo()));
		}
	}
}
